#pragma once
#include "DXAMLCommon.h"
#include "xamlcontext.h"
//#include "ObjectWriterStack.h"
#include "XamlSavedContext.h"

class ObjectWriterStack;
class XamlSavedContext;


class ObjectWriter;
class XamlCommonServiceProviderContext;



class ObjectWriterContext :
	public XamlContext,
	xenable_shared_from_this<ObjectWriterContext>
{
private: 
	ObjectWriterStack m_ObjectWriterStack;
	xshared_ptr<XamlQualifiedObject> m_qoRootInstance;
	xshared_ptr<XamlQualifiedObject> m_spEventRoot;
	xweak_ptr<ObjectWriter> m_spObjectWriter;
	xshared_ptr<XamlCommonServiceProviderContext> m_spXamlCommonServiceProviderContext;
	xshared_ptr<XamlType> m_spStyleXamlType;
	xshared_ptr<XamlType> m_spControlTemplateXamlType;
	xshared_ptr<XamlProperty> m_spStyleTargetTypeXamlProperty;
	xshared_ptr<XamlProperty> m_spControlTemplateTargetTypeProperty;
	xref_ptr<INameScope> m_spRootNamescope;
	xref_ptr<IPALUri> m_spBaseUri;
	xref_ptr<IPALUri> m_spXamlResourceUri;
	XUINT32 m_uiSavedDepth;
	XBOOL m_bAllowEvents;
	XBOOL m_bExpandTemplates;

	ObjectWriterContext(class xshared_ptr<XamlSchemaContext> spSchemaContext, XBOOL bAllowEvents, class xshared_ptr<XamlQualifiedObject> spEventRoot, class xshared_ptr<ObjectWriter> spObjectWriter, XBOOL bExpandTemplates);
public: 
	static HRESULT Create(const xshared_ptr<XamlSavedContext> & spXamlSavedContext, XBOOL bAllowEvents, class xshared_ptr<XamlQualifiedObject> spEventRoot, class xshared_ptr<ObjectWriter> spObjectWriter, XBOOL bExpandTemplates, xshared_ptr<ObjectWriterContext> & rspObjectWriterContext);
	static HRESULT Create(const xshared_ptr<XamlSchemaContext> & spXamlSavedContext, XBOOL bAllowEvents, class xshared_ptr<XamlQualifiedObject> spEventRoot, class xshared_ptr<ObjectWriter> spObjectWriter, XBOOL bExpandTemplates, xshared_ptr<ObjectWriterContext> & rspObjectWriterContext);
	virtual ~ObjectWriterContext();
	ObjectWriterFrame & Current();
	ObjectWriterFrame & Current() const;
	ObjectWriterFrame & Parent();
	ObjectWriterFrame & Parent() const;
	HRESULT ServiceProvider_ResolveXamlType(const struct XamlTypeToken & , xshared_ptr<XamlType> & rspXamlType);
	HRESULT ServiceProvider_ResolveXamlType(const xshared_ptr<CXString> & spName, xshared_ptr<XamlType> & rspXamlType);
	HRESULT ServiceProvider_GetAllAmbientValues(const xshared_ptr<XamlType> & spCeilingType, const xshared_ptr<XamlProperty> & spProperty, const xshared_ptr<XamlType> & spTypeToFind, enum CompressedObjectWriterStack::eCompressedStackCacheHint eCacheHint, xvector<xshared_ptr<XamlQualifiedObject> > & vecValues);
	HRESULT ServiceProvider_GetAmbientValue(const xshared_ptr<XamlType> & spCeilingType1, const xshared_ptr<XamlType> & spCeilingType2, const xshared_ptr<XamlProperty> & spProperty1, const xshared_ptr<XamlProperty> & spProperty2, const xshared_ptr<XamlType> & spTypeToFind, enum CompressedObjectWriterStack::eCompressedStackCacheHint cacheHint, xshared_ptr<XamlQualifiedObject> & rqoValue);
	HRESULT EnsureTargetTypeCaches();
	HRESULT ServiceProvider_GetAmbientTargetType(xshared_ptr<XamlQualifiedObject> & rqoValue);
	virtual HRESULT AddNamespacePrefix(const xshared_ptr<CXString> & inPrefix, const xshared_ptr<XamlNamespace> & spXamlNamespace);
	virtual HRESULT FindNamespaceByPrefix(const xshared_ptr<CXString> & inPrefix, xshared_ptr<XamlNamespace> & outNamespace);
	HRESULT get_TextSyntaxContext(xshared_ptr<XamlCommonServiceProviderContext> & spTextSyntaxContext) const;
	const xshared_ptr<XamlCommonServiceProviderContext> & get_MarkupExtensionContext() const;
	HRESULT PushScope();
	HRESULT PopScope();
	XBOOL IsStackEmpty();
	XINT32 get_Depth() const;
	XINT32 get_SavedDepth();
	XINT32 get_LiveDepth() const;
	XBOOL get_ExpandTemplates() const;
	XBOOL get_AllowEvents();
	const xshared_ptr<XamlQualifiedObject> & get_RootInstance();
	class xref_ptr<INameScope> get_RootNameScope();
	XBOOL get_CurrentIsPropertyAssigned(const xshared_ptr<XamlProperty> &);
	HRESULT NotifyPropertyAssigned(const xshared_ptr<XamlProperty> & inProperty, XBOOL & outAlreadyAssigned);
	HRESULT GetSavedContext(xshared_ptr<XamlSavedContext> & rspSavedContext);
	HRESULT GetFileInfo(XUINT32 *, XUINT32 *, xshared_ptr<CXString> &);
	const xref_ptr<IPALUri> & get_BaseUri();
	void set_BaseUri(class xref_ptr<IPALUri> spBaseUri);
	const xref_ptr<IPALUri> & get_XamlResourceUri();
	void set_XamlResourceUri(class xref_ptr<IPALUri> spXamlResourceUri);
private: 
	HRESULT FindNextAmbientValue(xvector<ObjectWriterFrame>::reverse_iterator & startingIterator, enum CompressedObjectWriterStack::eCompressedStackCacheHint cacheHint, const xshared_ptr<XamlType> & spCeilingType1, const xshared_ptr<XamlType> & spCeilingType2, const xshared_ptr<XamlProperty> & spProperty1, const xshared_ptr<XamlProperty> & spProperty2, const xshared_ptr<XamlType> & spTypeToMatch, XBOOL fStopOnFirst, xshared_ptr<XamlQualifiedObject> & rqoValue, XBOOL * pfStoppedOnCeiling);
	static HRESULT CheckAmbient(const xshared_ptr<XamlProperty> &);
	HRESULT GetStackIterator(xvector<ObjectWriterFrame>::const_reverse_iterator &, XUINT32);
	HRESULT GetStackIterator(xvector<ObjectWriterFrame>::reverse_iterator &, XUINT32);
public: 
	HRESULT Runtime_GetValue(const xshared_ptr<XamlQualifiedObject> & qoInstance, const xshared_ptr<XamlProperty> & spXamlProperty, xshared_ptr<XamlQualifiedObject> & rqoOut);
	HRESULT Runtime_SetValue(const xshared_ptr<XamlType> & inType, const xshared_ptr<XamlQualifiedObject> & inObj, const xshared_ptr<XamlProperty> & spProperty, const xshared_ptr<XamlQualifiedObject> & inValue);
	HRESULT Runtime_CreateInstance(const xshared_ptr<XamlType> & spXamlType, xshared_ptr<XamlQualifiedObject> & rqoOut);
	HRESULT Runtime_CreateFromValue(const xshared_ptr<XamlType> & spType, const xshared_ptr<XamlCommonServiceProviderContext> & inServiceContext, const xshared_ptr<XamlTextSyntax> & inTs, const xshared_ptr<XamlQualifiedObject> & qoValue, const xshared_ptr<XamlQualifiedObject> & qoRootInstance, xshared_ptr<XamlQualifiedObject> & qo);
	HRESULT Runtime_InitializationGuard(const xshared_ptr<XamlType> & spXamlType, const xshared_ptr<XamlQualifiedObject> & qoInstance, XBOOL fInitializing);
	HRESULT Runtime_Add(const xshared_ptr<XamlQualifiedObject> & qoParentCollection, const xshared_ptr<XamlProperty> & spCollectionProperty, const xshared_ptr<XamlQualifiedObject> & qoValue, const xshared_ptr<XamlType> & spValueType);
	HRESULT Runtime_AddToDictionary(const xshared_ptr<XamlQualifiedObject> & qoParentCollection, const xshared_ptr<XamlProperty> & spCollectionProperty, const xshared_ptr<XamlQualifiedObject> & qoValue, const xshared_ptr<XamlType> & spValueType, const xshared_ptr<XamlQualifiedObject> & qoKey);
	HRESULT Runtime_CallProvideValue(const xshared_ptr<XamlQualifiedObject> & qoMarkupExtension, const xshared_ptr<XamlCommonServiceProviderContext> & spServiceProviderContext, xshared_ptr<XamlQualifiedObject> & qoValue);
	HRESULT Runtime_SetUriBase(const xshared_ptr<XamlType> & spXamlType, const xshared_ptr<XamlQualifiedObject> & qoInstance, class xref_ptr<IPALUri> spBaseUri);
	HRESULT Runtime_SetConnectionId(const xshared_ptr<XamlQualifiedObject> & qoComponentConnector, const xshared_ptr<XamlQualifiedObject> & qoConnectionId, const xshared_ptr<XamlQualifiedObject> & qoTarget);
	HRESULT Runtime_GetResourcePropertyBag(const xshared_ptr<XamlQualifiedObject> & spKey, class xref_ptr<IPALUri> spBaseUri, const xshared_ptr<XamlQualifiedObject> & spObject, const xshared_ptr<XamlType> & spType, const xshared_ptr<XamlCommonServiceProviderContext> & spServiceProviderContext, xshared_ptr<xpairlist<xshared_ptr<XamlProperty>,xshared_ptr<XamlQualifiedObject> > > & spPropertyBag);
public: 
	static HRESULT CopyStack(const ObjectWriterStack & rstkFrom, ObjectWriterStack & rstkTo);
};

